Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

Reasonably Sound

TIME LIMIT = 1 SEC.

  • We don't always know what mysteries keep getting solved over at the Department of Mathematics, but we're sure it's a lot of fun. In any case, they constantly need a programmer's help to solve some complex problems. And, what do you know, they need our help yet again! This is what they want you to solve.
    Given a set of N pairs of numbers:
    • If one of the numbers is prime, and the other is even, the output should be such that when binary representation of the inputs is considered, the output bit should be 1, if and only if both the corresponding input bits are also 1.
    • If one of the numbers is prime, and the other is odd, the output should be such that when binary representation of the inputs is considered, the bit should be 1, if at least one of the corresponding input bits is also 1.
    • If both of the numbers are prime, the output should be such that when binary representation of the inputs is considered, the bit should be 1, if and only if one of the corresponding input bits is 1 and the other is 0.
    • If both the numbers are even, but none of them is prime, simply print a/b (i.e. a divided by b), where a is the first number in the pair and b is the second number in the pair.
    • If both the numbers are odd, but none of them is prime, simply print b/a (i.e. b divided by a), where a is the first number in the pair and b is the second number in the pair.
    • If one of the numbers is even and the other one is odd, but none of them is prime, simply print −1.
Input Output
First line will contain N, the number of pairs of numbers.
Each of the following N lines will contain two space-separated integers.
For each pair, output on a new line the result of the operation on that pair.
Constraints
  • 1 ≤ N ≤ 1000
  • 2 ≤ Each number in a pair ≤ 109
Example Test Case
Input             Output
3
2 4
3 9
5 7
0
11
2
Solution

#include <cstdio> //This function checks if a given number is a prime number or not. bool isPrime(long long int n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (long long int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } int main() { int n,temp; scanf("%d",&n); long long int a,b,c; long long sol[n]; temp = n; while(n--) { scanf("%lld%lld",&a,&b); if(isPrime(a) && isPrime(b)) //if both are prime, print XOR of a and b. { c = a ^ b; sol[temp-n-1] = c; } else if((isPrime(a) && b%2==0) || (isPrime(b) && a%2==0)) //If one is prime and other is even,print a&b { c = a & b; sol[temp-n-1] = c; } else if((isPrime(a) && b%2==1) || (isPrime(b) && a%2==1))//If one is prime and other is odd, print a|b { c = a | b; sol[temp-n-1] = c; } else if(a%2==0 && b%2==0) //if both are even, print a/b (integer division) { c = a / b; sol[temp-n-1] = c; } else if(a%2==1 && b%2==1)//if both are odd, print b/a (integer division) { c = b / a; sol[temp-n-1] = c; } else if((a%2==0 && b%2==1) || (a%2==1 && b%2==0)) //if one is even and other is odd, print -1 { sol[temp-n-1] = -1; } } for(int i=0;i<temp;i++) printf("%lld\n",sol[i]); return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();